#include "cardwidget.h"
#include "qpainter.h"
#include "QMouseEvent"
#include "qmatrix4x4.h"

//获取牌的颜色
int getMaskColor(int card)
{
    if(card>0&&card<=13)//梅花
    {
        return 0;
    }
    else if(card>13&&card<=26)//方块
    {
        return  1;
    }
    else if(card>26&&card<=39)//黑桃
    {
        return  2;
    }
    else if(card>39&&card<=52)//红桃
    {
        return  3;
    }
    return -1;
}


//基本构造函数
CardWidget::CardWidget(QWidget *parent) : QWidget(parent)
{

}
//带参数基本构造函数
//value 牌值 angle角度 isShow是否显示正面
CardWidget::CardWidget(int angle,bool isShow,QWidget *parent): QWidget(parent)
{
    this->angle=angle;

    this->isShow=isShow;
}

//当牌左键点击是调用此函数 是要是位置上升还是回复到原样
void CardWidget::setClick(bool b)
{
    isClick=b;
    if(isClick)
    {

            move(this->x(),this->y()-30);

    }
    else//回位
    {

            move(this->x(),this->y()+30);

    }
    //更新重绘
    update();
}

//当牌选中时，鼠标没有松开时调用，颜色改变
void CardWidget::setSelected(bool b)
{
    isSelected=b;
     //更新重绘
    update();
}
//控件重绘
void CardWidget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);//定义画板为自己背景，相当于VS中DC pDC
    //设置去毛边 高质量模式
    painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    //透明覆盖
    painter.fillRect(this->rect(), QBrush(Qt::transparent));


    //定义一个图像用来绘到背景
    QPixmap thisPix(width(),height());
    if(isShow)
    {
        if(num!=0)
        {
            thisPix.fill(Qt::transparent);//透明填充
            QPainter painter2;//重新定义一个画板为 thisPix
            painter2.begin(&thisPix);//画板开始
            //设置去毛边 高质量模式
            painter2.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
            //因为我图像大小是 122*150，为了能成比例，自适应缩放
            double dw=double(width())/122.00;
            double dh=double(height())/150.00;
            //3-2的绘制
            if(num>0&&num<16)
            {
/*
                //这是数字绘制的矩形框 x y w h   相当于VS中的 RECT
                QRect rect=QRect(int(dw*8),int(dh*13),int(dw*23),int(dh*31));
                //颜色 黑 红 黑 红顺序（梅花 方块 黑桃 红桃）
                //字符串数组
                QStringList colors={"black.png","red.png","black.png","red.png"};
                QStringList imgbgs={"bg_4.png","bg_3.png","bg_2.png","bg_1.png"};
                QStringList imgbg1={"bg_4_4.png","bg_3_3.png","bg_2_2.png","bg_1_1.png"};
                //导入背景图像
                QPixmap img(":/images/cards/"+colors[colors]);
                QPixmap img1(":/images/cards/"+imgbgs[colors]);
                QPixmap img11(":/images/cards/"+imgbg1[colors]);
                //如果不是 J Q K 绘入 3-10 A 2
                if(num<11 || num>13)
                {
                    painter2.drawPixmap(QRect(0,0,thisPix.width(),thisPix.height()),img1);
                    painter2.drawPixmap(rect,img,QRect(0,31*(num-3),23,31));
                }
                else//下面绘的J Q  K
                {
                    painter2.drawPixmap(QRect(0,0,thisPix.width(),thisPix.height()),img11);
                    QPixmap bgimg(":/images/cards/bg_"+QString::number(num)+".png");
                    painter2.drawPixmap(QRect(0,0,thisPix.width(),thisPix.height()),bgimg);
                    painter2.drawPixmap(rect,img,QRect(0,31*(num-3),23,31));
                }
                */
            }

            else//啥也不是
            {
                QPixmap bgimg(":/res/white.png");
                painter2.drawPixmap(QRect(0,0,thisPix.width(),thisPix.height()),bgimg);
            }
            if(isSelected)//如果选中了，上面添加一层透明度为100的颜色
            {
                painter2.setBrush(QColor(44,44,44,100));
                painter2.drawRoundedRect(rect(), 15*dw, 15*dh);
            }
            painter2.end();//画板结束
        }

    }
    else//不显示直接绘牌的背面
    {
        thisPix=QPixmap(":/res/CardBack.png");

    }

}
